﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_PicturesInfo。
    /// </summary>
    public class House_PicturesInfo
    {
        public House_PicturesInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_PicturesInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_PicturesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_PicturesInfo(");
            strSql.Append("RecordId,HouseId,PicType,ThumbnailUrl,OriginalUrl,MarkedUrl,Addtime,AddPerson,AddPersonId)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@HouseId,@PicType,@ThumbnailUrl,@OriginalUrl,@MarkedUrl,@Addtime,@AddPerson,@AddPersonId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "PicType", DbType.AnsiString, model.PicType);
            db.AddInParameter(dbCommand, "ThumbnailUrl", DbType.AnsiString, model.ThumbnailUrl);
            db.AddInParameter(dbCommand, "OriginalUrl", DbType.AnsiString, model.OriginalUrl);
            db.AddInParameter(dbCommand, "MarkedUrl", DbType.AnsiString, model.MarkedUrl);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_PicturesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_PicturesInfo set ");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("PicType=@PicType,");
            strSql.Append("ThumbnailUrl=@ThumbnailUrl,");
            strSql.Append("OriginalUrl=@OriginalUrl,");
            strSql.Append("MarkedUrl=@MarkedUrl,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddPersonId=@AddPersonId");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "PicType", DbType.AnsiString, model.PicType);
            db.AddInParameter(dbCommand, "ThumbnailUrl", DbType.AnsiString, model.ThumbnailUrl);
            db.AddInParameter(dbCommand, "OriginalUrl", DbType.AnsiString, model.OriginalUrl);
            db.AddInParameter(dbCommand, "MarkedUrl", DbType.AnsiString, model.MarkedUrl);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_PicturesInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

   
        /// <summary>
        /// 删除多条数据 ---chenjie
        /// </summary>
        /// <param name="RecoderIds">以 'a','b','c'.... 形式传入</param>
        public static int DeleteMore(string RecoderIds)
        {
            string strSQL = string.Format("delete House_PicturesInfo where RecordId in ({0});", RecoderIds);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            int count = db.ExecuteNonQuery(CommandType.Text, strSQL);
            return count;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_PicturesInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,PicType,ThumbnailUrl,OriginalUrl,MarkedUrl,Addtime,AddPerson,AddPersonId from House_PicturesInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.House_PicturesInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,PicType,ThumbnailUrl,OriginalUrl,MarkedUrl,Addtime,AddPerson,AddPersonId ");
            strSql.Append(" FROM House_PicturesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获取对应图片类型已经设置数量
        /// </summary>
        public static int GetHadSetPicCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(RecordId) ");
            strSql.Append(" FROM House_PicturesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DataSet ds = db.ExecuteDataSet(CommandType.Text, strSql.ToString());
            int result = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            return result;
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_PicturesInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_PicturesInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,PicType,ThumbnailUrl,OriginalUrl,MarkedUrl,Addtime,AddPerson,AddPersonId ");
            strSql.Append(" FROM House_PicturesInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_PicturesInfo> list = new List<Chenxin.Lib.House.Entity.House_PicturesInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_PicturesInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_PicturesInfo model = new Chenxin.Lib.House.Entity.House_PicturesInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.PicType = dataReader["PicType"].ToString();
            model.ThumbnailUrl = dataReader["ThumbnailUrl"].ToString();
            model.OriginalUrl = dataReader["OriginalUrl"].ToString();
            model.MarkedUrl = dataReader["MarkedUrl"].ToString();
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            return model;
        }

        #endregion  成员方法
    }
}

